package com.fstrat.frontend.proxy;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fstrat.backend.entities.Angebot;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class Utils implements IRATMessage{
	
	
	public static final String startTagUl = "<ul>";
	public static final String UL_TEXT = "Bitte beachten Sie:";
	public static final String endTagUl = "</ul>";
	public static final String startTagLi = "<li>";
	public static final String endTagLi = "</li>";
	public static final BigDecimal ZERO = BigDecimal.ZERO.setScale(2,BigDecimal.ROUND_HALF_UP);
	
	boolean sendeEmail(String betreff, String body)
	{
		throw new NotImplementedException();
	}

	static public String validateStringArgument(String value)
	{
		if(value == null) return "";
		else return value;
	}
	
	static public int validateIntArgument(String value)
	{
		if(value == null) return 0;
		else 
		{
			if(checkIfInt(value)) return  Integer.parseInt(value); else return 0;
		}
	}
	
	static public BigDecimal validateDecimalArgument(String value)
	{
		BigDecimal ret= ZERO;
		if(value != null) 
		{
			value = value.replaceAll("[,]", ".");
			ret = checkIfDecimal(value);
			if(ret!=null) return ret.setScale(2,BigDecimal.ROUND_HALF_UP);
			else ret = ZERO;
		}
		return ret;
	}
	
	public static String formatBigDecimal(BigDecimal bdWert) {
		DecimalFormat df = new DecimalFormat( "###,##0.00" ); 
		return df.format(bdWert.doubleValue());
	}
	
	public static Date formatDate(String date) throws ParseException{
		
		
		 Calendar cal = Calendar.getInstance();
		try {
				if(checkDate(date))
				{
					date.trim();
					String[] str = date.split("[.]");
					if(str.length==3)
					{
						cal.set(Integer.parseInt(str[2]),Integer.parseInt(str[1]),Integer.parseInt(str[0]));
						return cal.getTime();
					}

				}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	static public Date addFieldToDate(Date d, int field, int amount)
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);		
		cal.add(field, amount);
		return cal.getTime();
	}
	
	static public  String validateAngebot(Angebot a) throws ParseException
	{
		String errorPanel = "";
		Calendar calAblaufDatum = Calendar.getInstance(), calHeute = Calendar.getInstance();
		
		calHeute.setTime(new Date());
		
		if("".equals(a.getTitel())) errorPanel += startTagLi + ERROR_ANGEBOT_TITLE_MISSING+ endTagLi;
		if(a.getAblaufdatum()== null) errorPanel += startTagLi + ERROR_ANGEBOT_BIS_DATE_MISSING+ endTagLi;
		else 
		{
			calAblaufDatum.setTime(a.getAblaufdatum());
			calAblaufDatum.add(Calendar.MONTH, -1);
			if(calAblaufDatum.compareTo(calHeute) < 0)
				errorPanel += startTagLi + ERROR_ANGEBOT_BIS_DATE_BEFOR_TODAY + endTagLi;
		}
		
		if(a.getMaxVerleihdauer() == 0) errorPanel += startTagLi + ERROR_ANGEBOT_MAX_DURATION_MISSING+ endTagLi;
		else
			if(a.getMaxVerleihdauer() < 0 || a.getMaxVerleihdauer() > 90) errorPanel += startTagLi + ERROR_ANGEBOT_MAX_DURATION_NOT_IN_RANGE+ endTagLi;
			else
			{
				calHeute.add(Calendar.DATE, a.getMaxVerleihdauer());
			}
				if(a.getAblaufdatum()!= null && calAblaufDatum.compareTo(calHeute) < 0)
					errorPanel += startTagLi + ERROR_ANGEBOT_BIS_DATE_PLUS_MAX_DURATION_BEFOR_TODAY + endTagLi;
		
		if(new  BigDecimal(a.getPreis()).setScale(2).equals(ZERO)) 
			errorPanel +=  startTagLi + ERROR_ANGEBOT_PRICE_MISSING+ endTagLi;
		else
		{
			if(new  BigDecimal(a.getPreis()).setScale(2).compareTo(ZERO) < 0)  errorPanel +=  startTagLi + ERROR_ANGEBOT_PRICE_NEGATIV+ endTagLi;
		}
		
		if("".equals(a.getBeschreibung())) errorPanel += startTagLi + ERROR_ANGEBOT_DESC_MISSING + endTagLi;
		
		if(!"".equals(errorPanel)) errorPanel = startTagUl + UL_TEXT + errorPanel + endTagUl;
		else return "";
		return errorPanel;
	}
	
	public static boolean checkDate(String dateStr)
	 {
		 	Pattern datePatt = Pattern.compile("([0-9]{2}).([0-9]{2}).([0-9]{4})");
			Matcher m = datePatt.matcher(dateStr);
			return m.matches();
	 }
	
	static BigDecimal checkIfDecimal(String in) {
	        
	        try {
	           return new BigDecimal(in);
	        
	        } catch (NumberFormatException ex) {
	            return null;
	        }
	        
	    }

	static boolean checkIfInt(String in) {
        
        try {

            Integer.parseInt(in);
        
        } catch (NumberFormatException ex) {
            return false;
        }
        
        return true;
    }

	
	 public static String now() {
	        Calendar cal = Calendar.getInstance();
	        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
	        return sdf.format(cal.getTime());
	 }
	 
	 public static Date nowAsDate() {
	        Calendar cal = Calendar.getInstance();
	        return cal.getTime();
	 }
	 
	 
	 public static String getDatumAufbereitet(Date d)
	 {
		 
		 String ret="";
		 if(d!=null)
		 {
			 Calendar c = Calendar.getInstance();
			 c.setTime(d);
			 ret = c.get(Calendar.DATE) + "." + c.get(Calendar.MONTH) + "." + c.get(Calendar.YEAR); 
		 }
		 return ret;
	 }
	

}
